package com.framework.common.util.response;

import com.alibaba.fastjson.JSONObject;
import com.framework.common.model.response.R;
import com.framework.common.util.other.CodingUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.common.util.response
 * @description 返回公用类
 * @datetime 2022-01-09 11:11:11
 */
public class ResponseUtil {
    private static Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    public static void out(HttpServletResponse response, R r) {
        try {
            response.setCharacterEncoding(CodingUtil.CODING_UTF8);
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            response.getOutputStream().write(JSONObject.toJSONBytes(r));
//            response.getOutputStream().flush();
//            response.getOutputStream().close();
//            response.getWriter().flush();
//            response.getWriter().close();
        } catch (Exception e) {
            log.error(e + "，输出JSON出错");
        }
    }
}
